﻿using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Acme.BookStore.Captchas;
using Volo.Abp.Domain.Repositories;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Acme.BookStore.RBAC;
using Acme.BookStore.Users;
using AutoMapper.Internal.Mappers;
using Volo.Abp.ObjectMapping;
using Pomelo.EntityFrameworkCore.MySql.Query.Internal;
using Acme.BookStore.Captchass;
using Microsoft.CodeAnalysis.Text;
using Microsoft.AspNetCore.Authorization;

namespace Acme.BookStore.Controllers
{
    /// <summary>
    /// 生成验证码控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CaptchasController : ControllerBase
    {
        private readonly ICaptchaServices captchaRepository;

        public CaptchasController(ICaptchaServices captchaRepository)
        {
            this.captchaRepository = captchaRepository;
        }
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<FileContentResult> GenerateCaptcha()
        {
            // 生成随机验证码
                string captchaCode = GenerateRandomCode(6);
            CaptchaDTO captcha = new CaptchaDTO();
            captcha.Code = captchaCode;

            await captchaRepository.GetByCodeAsync(captcha);
            // 创建图片
            using (Bitmap bitmap = new Bitmap(200, 80))
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                // 设置背景颜色
                graphics.Clear(Color.White);

                // 设置字体和颜色
                Font font = new Font("Arial", 24, FontStyle.Bold);
                Brush brush = new SolidBrush(Color.Black);

                // 绘制验证码
                graphics.DrawString(captchaCode, font, brush, 50, 22);

                // 添加干扰线
                for (int i = 0; i < 5; i++)
                {
                    int x1 = new Random().Next(bitmap.Width);
                    int y1 = new Random().Next(bitmap.Height);
                    int x2 = new Random().Next(bitmap.Width);
                    int y2 = new Random().Next(bitmap.Height);
                    graphics.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                // 将图片保存到内存流
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    bitmap.Save(memoryStream, ImageFormat.Jpeg);
                    memoryStream.Position = 0;

                    // 返回图片
                    return File(memoryStream.ToArray(), "image/jpeg");
                }
            }
        }

        private string GenerateRandomCode(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var random = new Random();
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }


       
    }
}
